/**
 * @fileoverview Tests for jUnit Formatter.
 * @author Jamund Ferguson
 *
 * Updated for use with sass-lint under MIT licence
 * @license https://github.com/sasstools/sass-lint/blob/master/lib/format/LICENSE
 */

'use strict';

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const assert = require('chai').assert,
    formatter = require('../../lib/format/formatters/junit');

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

describe('formatter:junit', () => {
  describe('when there are no problems', () => {
    const code = [];

    it('should not complain about anything', () => {
      const result = formatter(code);

      assert.equal(result.replace(/\n/g, ''), '<?xml version="1.0" encoding="utf-8"?><testsuites></testsuites>');
    });
  });

  describe('when passed a single message', () => {
    const code = [{
      filePath: 'foo.scss',
      messages: [{
        message: 'Unexpected foo.',
        severity: 2,
        line: 5,
        column: 10,
        ruleId: 'foo'
      }]
    }];

    it('should return a single <testcase> with a message and the line and col number in the body (error)', () => {
      const result = formatter(code);

      assert.equal(result.replace(/\n/g, ''), '<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.sassLint" time="0" tests="1" errors="1" name="foo.scss"><testcase time="0" name="org.sassLint.foo"><failure message="Unexpected foo."><![CDATA[line 5, col 10, error - Unexpected foo. (foo)]]></failure></testcase></testsuite></testsuites>');
    });

    it('should return a single <testcase> with a message and the line and col number in the body (warning)', () => {
      code[0].messages[0].severity = 1;
      const result = formatter(code);

      assert.equal(result.replace(/\n/g, ''), '<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.sassLint" time="0" tests="1" errors="1" name="foo.scss"><testcase time="0" name="org.sassLint.foo"><failure message="Unexpected foo."><![CDATA[line 5, col 10, warning - Unexpected foo. (foo)]]></failure></testcase></testsuite></testsuites>');
    });
  });

  describe('when passed multiple messages', () => {
    const code = [{
      filePath: 'foo.scss',
      messages: [{
        message: 'Unexpected foo.',
        severity: 2,
        line: 5,
        column: 10,
        ruleId: 'foo'
      }, {
        message: 'Unexpected bar.',
        severity: 1,
        line: 6,
        column: 11,
        ruleId: 'bar'
      }]
    }];

    it('should return a multiple <testcase>\'s', () => {
      const result = formatter(code);

      assert.equal(result.replace(/\n/g, ''), '<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.sassLint" time="0" tests="2" errors="2" name="foo.scss"><testcase time="0" name="org.sassLint.foo"><failure message="Unexpected foo."><![CDATA[line 5, col 10, error - Unexpected foo. (foo)]]></failure></testcase><testcase time="0" name="org.sassLint.bar"><failure message="Unexpected bar."><![CDATA[line 6, col 11, warning - Unexpected bar. (bar)]]></failure></testcase></testsuite></testsuites>');
    });
  });

  describe('when passed special characters', () => {
    const code = [{
      filePath: 'foo.scss',
      messages: [{
        message: 'Unexpected <foo></foo>\b\t\n\f\r牛逼.',
        severity: 1,
        line: 5,
        column: 10,
        ruleId: 'foo'
      }]
    }];

    it('should make them go away', () => {
      const result = formatter(code);

      assert.equal(result.replace(/\n/g, ''), '<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.sassLint" time="0" tests="1" errors="1" name="foo.scss"><testcase time="0" name="org.sassLint.foo"><failure message="Unexpected &lt;foo&gt;&lt;/foo&gt;&#8;&#9;&#10;&#12;&#13;&#29275;&#36924;."><![CDATA[line 5, col 10, warning - Unexpected &lt;foo&gt;&lt;/foo&gt;&#8;&#9;&#10;&#12;&#13;&#29275;&#36924;. (foo)]]></failure></testcase></testsuite></testsuites>');
    });
  });

  describe('when passed multiple files with 1 message each', () => {
    const code = [{
      filePath: 'foo.scss',
      messages: [{
        message: 'Unexpected foo.',
        severity: 1,
        line: 5,
        column: 10,
        ruleId: 'foo'
      }]
    }, {
      filePath: 'bar.scss',
      messages: [{
        message: 'Unexpected bar.',
        severity: 2,
        line: 6,
        column: 11,
        ruleId: 'bar'
      }]
    }];

    it('should return 2 <testsuite>\'s', () => {
      const result = formatter(code);

      assert.equal(result.replace(/\n/g, ''), '<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.sassLint" time="0" tests="1" errors="1" name="foo.scss"><testcase time="0" name="org.sassLint.foo"><failure message="Unexpected foo."><![CDATA[line 5, col 10, warning - Unexpected foo. (foo)]]></failure></testcase></testsuite><testsuite package="org.sassLint" time="0" tests="1" errors="1" name="bar.scss"><testcase time="0" name="org.sassLint.bar"><failure message="Unexpected bar."><![CDATA[line 6, col 11, error - Unexpected bar. (bar)]]></failure></testcase></testsuite></testsuites>');
    });
  });

  describe('when passed multiple files should print even if no errors', () => {
    const code = [{
      filePath: 'foo.scss',
      messages: [{
        message: 'Unexpected foo.',
        severity: 1,
        line: 5,
        column: 10,
        ruleId: 'foo'
      }]
    }, {
      filePath: 'bar.scss',
      messages: []
    }];

    it('should return 2 <testsuite>', () => {
      const result = formatter(code);

      assert.equal(result.replace(/\n/g, ''), '<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite package="org.sassLint" time="0" tests="1" errors="1" name="foo.scss"><testcase time="0" name="org.sassLint.foo"><failure message="Unexpected foo."><![CDATA[line 5, col 10, warning - Unexpected foo. (foo)]]></failure></testcase></testsuite><testsuite package="org.sassLint" time="0" tests="0" errors="0" name="bar.scss"></testsuite></testsuites>');
    });
  });
});
